﻿#region MIT

//  /*The MIT License (MIT)
// 
//  Copyright 2016 lizs lizs4ever@163.com
//  
//  Permission is hereby granted, free of charge, to any person obtaining a copy
//  of this software and associated documentation files (the "Software"), to deal
//  in the Software without restriction, including without limitation the rights
//  to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
//  copies of the Software, and to permit persons to whom the Software is
//  furnished to do so, subject to the following conditions:
//  
//  The above copyright notice and this permission notice shall be included in
//  all copies or substantial portions of the Software.
//  
//  THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
//  IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
//  FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
//  AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
//  LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
//  OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
//  THE SOFTWARE.
//   * */

#endregion

using System;
using System.Collections.Generic;
using System.Linq;

namespace mom
{
    /// <summary>
    ///     collection extend
    /// </summary>
    public static class Enumerable
    {
        /// <summary>
        ///     whether is null or empty collection
        /// </summary>
        /// <typeparam name="T"></typeparam>
        /// <param name="enumerable"></param>
        /// <returns></returns>
        public static bool IsNullOrEmpty<T>(this IEnumerable<T> enumerable)
        {
            if (enumerable == null)
                return true;

            var collection = enumerable as ICollection<T>;
            if (collection != null)
                return collection.Count == 0;

            return !enumerable.Any();
        }

        /// <summary>
        ///     add multiple ranges
        /// </summary>
        /// <typeparam name="T"></typeparam>
        /// <param name="source"></param>
        /// <param name="ranges"></param>
        /// <returns></returns>
        public static void AddRanges<T>(this List<T> source, params IEnumerable<T>[] ranges)
        {
            if (ranges.IsNullOrEmpty()) return;
            foreach (var enumerable in ranges)
            {
                var collection = enumerable as T[] ?? enumerable?.ToArray();
                if (!collection.IsNullOrEmpty())
                {
                    source.AddRange(collection);
                }
            }
        }

        /// <summary>
        /// </summary>
        /// <typeparam name="TKey"></typeparam>
        /// <typeparam name="TValue"></typeparam>
        /// <param name="dict"></param>
        /// <returns></returns>
        public static IEnumerable<TValue> RandomValues<TKey, TValue>(this IDictionary<TKey, TValue> dict)
        {
            var rand = new Random();
            var values = dict.Values.ToList();
            if (values.IsNullOrEmpty()) yield break;
            var size = dict.Count;
            while (true)
            {
                var ret = values[rand.Next(size)];
                values.Remove(ret);
                yield return ret;
            }
        }

        /// <summary>
        ///     return a random value from dic
        /// </summary>
        /// <typeparam name="TKey"></typeparam>
        /// <typeparam name="TValue"></typeparam>
        /// <param name="dict"></param>
        /// <returns></returns>
        public static TValue RandomValue<TKey, TValue>(this IDictionary<TKey, TValue> dict)
        {
            var rand = new Random();
            var values = dict.Values.ToList();

            return values.IsNullOrEmpty() ? default(TValue) : values[rand.Next(dict.Count)];
        }

        /// <summary>
        ///     return an random key from dic
        /// </summary>
        /// <typeparam name="TKey"></typeparam>
        /// <typeparam name="TValue"></typeparam>
        /// <param name="dict"></param>
        /// <returns></returns>
        public static TKey RandomKey<TKey, TValue>(this IDictionary<TKey, TValue> dict)
        {
            var rand = new Random();
            var keys = dict.Keys.ToList();
            if (keys.IsNullOrEmpty()) throw new Exception("Empty dictinary.");
            return keys[rand.Next(dict.Count)];
        }
    }
}
